// 用于存储 文件名称，url,是否启用 等关联信息

class FilesRepository {
  constructor(dao) {
    this.dao = dao
  }

  // status 默认 0 禁用，1 启用，2 已删除
  // timestamp 上传时间戳

  // 没有关联 projectId 的文件保留三天后自动清理 (直接删除)

  createTable() {
    const sql = `
          CREATE TABLE IF NOT EXISTS files (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            name TEXT,
            url TEXT,
            timestamp INTEGER,
            status INTEGER DEFAULT 0,
            download INTEGER DEFAULT 0,
            projectId INTEGER
            )`
    return this.dao.run(sql)
  }

  create(task) {
    const { 
      name,
      url,
      timestamp
    } = task;

    return this.dao.run(
      `INSERT INTO files (name, url, timestamp)
        VALUES (?, ?, ?)`,
      [
        name,
        url,
        timestamp
      ])
  }

  update(task) {
    const {
      status,
      projectId,
      id
    } = task

    return this.dao.run(
      `UPDATE files
        SET status = ? ,
          projectId = ? 
        WHERE id = ?`,
      [
        status,
        projectId,
        id
      ])
  }

  delete(id) {
    return this.dao.run(
      `DELETE FROM files WHERE id = ?`,
      [id]
    )
  }

  getById(id) {
    return this.dao.get(
      `SELECT * FROM files WHERE id = ?`,
      [id])
  }

  getAll() {
    return this.dao.all(`SELECT * FROM files`)
  }
}

module.exports = FilesRepository;